package com.china08.yxyapi.api.schwebsite.comm;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSetting;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.db.SwsSettingRepository;

@CrossOrigin
@RestController("SwsCommTouchUsController")
public class SwsCommTouchUsController extends YxyApiBaseController {

    @Autowired
    private RestTemplate restTemplate;

    @Value(value = "${yx.rest.api.yx.path}")
    private String yxyApiYxUrl;

    @Autowired
    private SwsSettingRepository swsSettingRepository;

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @RequestMapping(value = "/api_yw/schwebsite/comm/touchus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String domainName = getDomainName(request);
        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains byDomainName = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != byDomainName, "域名对象为空");

        String schoolId = byDomainName.getSchoolId();

        SwsSetting settings = swsSettingRepository.findBySchoolId(schoolId);
        Assert.isTrue(null != settings, "学校对象为空");
        RespModel model = new RespModel();
        model.address = settings.getAddress();
        model.telephone = settings.getTelephone();
        model.description = settings.getDescription();
        Map<String, String> map = restTemplate.getForObject(yxyApiYxUrl + "/comm/school/" + schoolId, Map.class);
        model.geoLat = map.get("geoLat");
        model.geoLng = map.get("geoLng");
        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {

        public String address;

        public String telephone;

        public String description;

        public String geoLat;

        public String geoLng;

    }

}
